<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// import echarts from 'echarts'
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        color: 'rgba(38, 147, 255, 1)',
        title: {
          text: '学历分布统计',
          left: 'left',
          top: '0%',
          textStyle: {
            color: '#000', // 标题颜色
            fontWeight: 'bold',
            fontSize: 16
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: function(params) {
            return params[0].name + ': ' + params[0].value
          }
        },
        grid: {
          top: 60,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['博士', '硕士', '大学本科', '大专', '中专', '技校', '职高'],
          axisTick: {
            show: false,
            alignWithLabel: true
          },
          axisLabel: {
            textStyle: {
              color: 'rgba(0, 0, 0, 0.40)' // 这里设置颜色
            },
            interval: 0 // 强制显示完整
          }
        }],
        yAxis: [{
          // name:'人数',
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: 'rgba(0, 0, 0, 0.40)' // 这里设置颜色
            },
            interval: 0 // 强制显示完整
          }

        }],
        series: [{
          name: '人数',
          type: 'pictorialBar',
          // stack: 'vistors',
          barWidth: '100%',
          barCategoryGap: '-30px',
          data: [390, 330, 220, 334, 390, 330, 220],
          // 自定义svg 图标 (三角锥形的关键)
          symbol: 'path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z',
          itemStyle: {
            label: {
              show: true
            },
            // borderColor: 'rgba(38, 147, 255, 0.7)',
            color: {
              colorStops: [
                {
                  offset: 0.9,
                  color: 'rgba(38, 147, 255, 1)'
                },
                {
                  offset: 0.5,
                  color: 'rgba(38, 147, 255, 0.40)'
                }
              ]
            }
          },

          animationDuration,
          label: {
            normal: {
              position: 'inside', // 在内部显示，inside、 outseide 是在外部显示
              show: false
            }
          }
        }]
      })
    }
  }
}
</script>
